#include <iostream>
#include <stdio.h>
#include "common/common.h"
#include "config/config.h"
#include "session/session_manager.h"
#include "accept/accept.h"
#include "session/session_machine/state_machin_define.h"
#include "session/session_machine/session_machine.h"


void Test(int argc, char* argv[])
{
    std::cout << ArrowWebCodeView::AppConfig::Instance()->Port() << std::endl;
    std::cout << ArrowWebCodeView::AppConfig::Instance()->DefalutFolder() << std::endl;
    printf("Hello, from web_code_view!\n");

    // std::cout << beast::websocket::stream<tcp::socket>::write_message_max << std::endl;
    // std::cout << beast::websocket::stream<tcp::socket>::read_message_max << std::endl;
    ArrowWebCodeView::SessionMachine::Data data;
    data.strPassword = "123456";

    ArrowWebCodeView::SessionMachine::Context context;
    context.Init(data, ArrowWebCodeView::SessionMachine::StateEnum::CheckPassword);
    // context.Init();

    std::string strIn;
    std::string strOut;
    strIn = "123456";
    auto ret = context.HandleMsg(ArrowWebCodeView::SessionMachine::MsgEnum::Login, strIn, strOut);
    std::cout << " strIn: " << strIn << "\n"
              << "ret: " << ArrowWebCodeView::SessionMachine::StateEnumStr::ItemStr(ret) << "\n"
              << " strOut: " << strOut << "\n"
              << std::endl;

    strIn = "/home/zhuyb/Documents/1.code/web_code_view/server/";
    context.HandleMsg(ArrowWebCodeView::SessionMachine::MsgEnum::Folder, strIn, strOut);
    std::cout << " strIn: " << strIn << "\n"
              << "ret: " << ArrowWebCodeView::SessionMachine::StateEnumStr::ItemStr(ret) << "\n"
              << " strOut: " << strOut << "\n"
              << std::endl;

    strIn = "/home/zhuyb/Documents/1.code/web_code_view/server/session/session.h";
    context.HandleMsg(ArrowWebCodeView::SessionMachine::MsgEnum::File, strIn, strOut);
    std::cout << " strIn: " << strIn << "\n"
              << "ret: " << ArrowWebCodeView::SessionMachine::StateEnumStr::ItemStr(ret) << "\n"
              << " strOut: " << strOut << "\n"
              << std::endl;
}

int main(int argc, char* argv[])
{
    ArrowLog::init(nullptr);

    std::string strFolder = Arrow::Other::GetDirectoryPath(argv[0]);
    ArrowWebCodeView::AppConfig::Instance()->Read(strFolder.c_str());

    // Test(argc, argv);
    asio::io_context ioContext;

    ArrowWebCodeView::AppSessionManger::Instance()->Init(ioContext);
    ArrowWebCodeView::AppSessionManger::Instance()->Run();

    try
    {
        ArrowWebCodeView::CAccept accept(ioContext, tcp::endpoint(tcp::v4(), 60004));
        ARROW_LOG_INFO_FMT("accept %d success", 60004);
        ioContext.run();
        return 0;
    }
    catch (const std::exception& e)
    {
        ARROW_LOG_ERROR_FMT("accept %d fail. error msg:%s", 60004, e.what());
    }
    return 0;
}
